package com.bdcom.autotest.ui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileWriter;
import java.io.IOException;
import java.util.List;
import java.util.Map;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import javax.swing.JSeparator;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import com.bdcom.autotest.interaction.bean.DeviceUpdBean;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.bdcom.autotest.BootThread;
import com.bdcom.autotest.MainFrame;
import com.bdcom.autotest.interaction.socket.DevSocket;
import com.bdcom.autotest.interaction.step.FlowBean;
import com.bdcom.autotest.interaction.step.StepThread;
import com.bdcom.autotest.ui.snp.ISNGetSet;
import com.bdcom.autotest.util.UIUtil;

public class LeftPanel extends JPanel implements ActionListener {

	private static Logger log = LoggerFactory.getLogger(BootThread.class);
	
	private MainFrame mf;
	private JPanel panel ;
	
	private JPanel snPanel1 ;
	
	private JPanel logPanel ;
	private JTextArea logArea;
	private JScrollPane logScroll ;
	
	
	private JButton startBtn;
	private JButton nextBtn;
	private JButton restartBtn;
	private JButton exportBtn;
	
	public LeftPanel( MainFrame mf ) {
		this.mf = mf;
		
		this.setPreferredSize(new Dimension( 540 , 0 ) );
		
		this.setLayout(new BorderLayout() );
		
		panel = new JPanel(  );
		this.add(panel);
		JSeparator createVSep = UIUtil.createVSep();
		this.add(createVSep , BorderLayout.EAST);
		initComponent();
		
		UIUtil.setCompBack(panel);
		UIUtil.setCompBack(this);
		
	}

	private void initComponent() {
		panel.setLayout(new BorderLayout());
		
		snPanel1 =createSNPanel1();
		
		panel.add(snPanel1 , BorderLayout.NORTH);
		
		logPanel = UIUtil.createPanel();
		logPanel.setLayout(new BorderLayout());
		logArea = new JTextArea( getLogStr() );
		logArea.setLineWrap(true);
		logArea.setEditable(false);
		logScroll = new JScrollPane(logArea);
		logScroll.setBorder( BorderFactory.createLineBorder(Color.BLACK) );
		logScroll.setBackground(Color.white);
		logPanel.add(logScroll);
		logPanel.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
		
		JLabel logLabel = new JLabel("当前日志 : ");
		logPanel.add(logLabel , BorderLayout.NORTH);
		panel.add( logPanel );
		
		startBtn = new JButton("开始工单");
		startBtn.addActionListener( this );

		nextBtn = new JButton("继续当前工单");
		nextBtn.addActionListener( this );
		JPanel btnPanel = UIUtil.createPanel();
		restartBtn = new JButton("开始新的工单");
//		exportBtn = new JButton("导出日志");
//		exportBtn.addActionListener( this );
		btnPanel.add(startBtn);
		btnPanel.add(nextBtn);
		btnPanel.add(restartBtn);
//		btnPanel.add(exportBtn);
		restartBtn.addActionListener(this);
		restartBtn.setEnabled(false);
		nextBtn.setEnabled(false);
		btnPanel.setPreferredSize(new Dimension(  0 , 50 ));
		panel.add(btnPanel ,BorderLayout.SOUTH  );
	}

	JTextField sn1;
	private JPanel createSNPanel1() {
		JPanel createPanel = UIUtil.createPanel();
		createPanel.setPreferredSize(new Dimension( 0 , 240 ));
		createPanel.setLayout( new FlowLayout(FlowLayout.LEFT ) );
		Font font2 = new Font("Dialog",Font.PLAIN, 14);
		JLabel jLabel = new JLabel("SN:");
		jLabel.setFont(font2);
		sn1 = new JTextField(20);
		sn1.setFont(font2);
//		createPanel.add(jLabel);
//		createPanel.add(sn1);
		return createPanel;
	}
	
	
	@Override
	public void actionPerformed(ActionEvent e) {
		if( e.getSource() == startBtn   ) {
			
			startTestBtn();
			
			new Thread() {
				public void run() {
					
					StepThread stepThread = new StepThread(mf);
//					DevSocket devSocket = new DevSocket(stepThread);
//					if( !devSocket.isOk()   ) {
//						stepThread.addErrorLog( "无法与设备建立socket连接请检查设备是否正常启动！");
//						endTestBtn(  );
//					}else {
//						stepThread.setDevSokcet(devSocket);
//						stepThread.start();
//					}
					
					stepThread.start();
				}
			}.start();
			
		}else if(  e.getSource() == restartBtn   ) {
			clearAndRestart();
		}else if(  e.getSource() == nextBtn ){
			StepThread thread = mf.getCurretThread();
			String workOrderNum = thread.getWorkOrderNum();
			Map<String, DeviceUpdBean> macToUpdBean = thread.getMacToUpdBean();
			clearAndRestart();
			startTestBtn();
			new Thread() {
				public void run() {

					StepThread stepThread = new StepThread(mf);
//					DevSocket devSocket = new DevSocket(stepThread);
//					if( !devSocket.isOk()   ) {
//						stepThread.addErrorLog( "无法与设备建立socket连接请检查设备是否正常启动！");
//						endTestBtn(  );
//					}else {
//						stepThread.setDevSokcet(devSocket);
//						stepThread.start();
//					}

					stepThread.start();
					stepThread.setNewWorkOrder(false);
					stepThread.setWorkOrderNum(workOrderNum);
					setworkno(workOrderNum);//设置左侧显示
					stepThread.setMacToUpdBean(macToUpdBean);
				}
			}.start();
		}else if(  e.getSource() == exportBtn ){
			exportLog();
		}
	}
	private void exportLog(){
		try {
//			String filename = System.currentTimeMillis();
			FileWriter pw = new FileWriter (System.currentTimeMillis()+"log.txt");
			logArea.write(pw);
			JOptionPane.showMessageDialog( mf ,"导出完成！","信息",JOptionPane.INFORMATION_MESSAGE );
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} //Object of JTextArea
	}
	
	private void clearAndRestart() {
		endTestBtn();
		mf.startANewTest();
		logArea.setText("");
	}

	private String getLogStr() {
		String srt = "";
		return srt;
	}
	
	
	
	public void setflow(FlowBean flowBean) {
		String snPanelClass = flowBean.getSnPanelClass();
		try {
			Class<?> forName = Class.forName(snPanelClass);
			Object newInstance = forName.newInstance();
			if(  newInstance instanceof  JPanel ) {
				JPanel pp = (JPanel) newInstance;
				
				panel.remove(snPanel1);
				panel.add(pp , BorderLayout.NORTH);
				snPanel1 = pp;
				panel.revalidate();
			}
			
		} catch ( Exception e) {
			log.error("" , e);
		}
	}
	

	public void addLog(String string) {
		
		logArea.append(string);
		
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				JScrollBar verticalScrollBar = logScroll.getVerticalScrollBar();
				int maximum = verticalScrollBar.getMaximum();
				verticalScrollBar.setValue( maximum );
			}
		});
		
	}


	public void startTestBtn( ) {
		startBtn.setEnabled(false);
		nextBtn.setEnabled(true);
		restartBtn.setEnabled(true);
		mf.setCheckBox( false );
	}
	
	//此处2个end先后有问题，暂记
	public void endTestBtn( ) {
		restartBtn.setEnabled(false);
		nextBtn.setEnabled(false);
		startBtn.setEnabled(true);
		mf.setCheckBox( true );
	}
	public void enableStartBtn(){
		nextBtn.setEnabled(true);
		restartBtn.setEnabled(true);
		startBtn.setEnabled(false);
	}
	public void emptysn() {
		if(  snPanel1 instanceof ISNGetSet ) {
			ISNGetSet getset = (ISNGetSet) snPanel1;
			getset.setSNs(null);
		}
	}
	
	public String[] getsns() {
		if(  snPanel1 instanceof ISNGetSet ) {
			ISNGetSet getset = (ISNGetSet) snPanel1;
			String[] sNs = getset.getSNs();
			return sNs;
		}
		return null;
	}
	
	public void setsns( String[] sns) {
		if(  snPanel1 instanceof ISNGetSet ) {
			ISNGetSet getset = (ISNGetSet) snPanel1;
			getset.setSNs(sns);
		}
	}

	public void setworkno(String workNo) {
		if(  snPanel1 instanceof ISNGetSet ) {
			ISNGetSet getset = (ISNGetSet) snPanel1;
			getset.setWorkNo(workNo);
		}
	}

	public String getworkno() {
		if(  snPanel1 instanceof ISNGetSet ) {
			ISNGetSet getset = (ISNGetSet) snPanel1;
			String sNs = getset.getWorkNo();
			return sNs;
		}
		return null;
	}

	public void setFail(List<String> sns) {
		if(  snPanel1 instanceof ISNGetSet ) {
			ISNGetSet getset = (ISNGetSet) snPanel1;
			getset.setFail(sns);
		}
	}

	public void setResult(List<String> sns) {
		if(  snPanel1 instanceof ISNGetSet ) {
			ISNGetSet getset = (ISNGetSet) snPanel1;
			getset.setResult(sns);
		}
	}

}
